package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.CrmTitle;
import com.ktgj.customer.service.ICrmTitleService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * CRM跑马灯维护Controller
 * 
 * @author ruoyi
 * @date 2022-08-05
 */
@RestController
@RequestMapping("/title")
public class CrmTitleController extends BaseController
{
    @Autowired
    private ICrmTitleService crmTitleService;

    /**
     * 查询CRM跑马灯维护列表
     */
    @PreAuthorize(hasPermi = "system:title:list")
    @GetMapping("/list")
    public TableDataInfo list(CrmTitle crmTitle)
    {
        startPage();
        List<CrmTitle> list = crmTitleService.selectCrmTitleList(crmTitle);
        return getDataTable(list);
    }

    /**
     * 导出CRM跑马灯维护列表
     */
    @PreAuthorize(hasPermi = "system:title:export")
    @Log(title = "CRM跑马灯维护", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CrmTitle crmTitle) throws IOException
    {
        List<CrmTitle> list = crmTitleService.selectCrmTitleList(crmTitle);
        ExcelUtil<CrmTitle> util = new ExcelUtil<CrmTitle>(CrmTitle.class);
        util.exportExcel(response, list, "CRM跑马灯维护数据");
    }

    /**
     * 获取CRM跑马灯维护详细信息
     */
    @PreAuthorize(hasPermi = "system:title:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmTitleService.selectCrmTitleById(id));
    }

    /**
     * 新增CRM跑马灯维护
     */
    @PreAuthorize(hasPermi = "system:title:add")
    @Log(title = "CRM跑马灯维护", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmTitle crmTitle)
    {
        return toAjax(crmTitleService.insertCrmTitle(crmTitle));
    }

    /**
     * 修改CRM跑马灯维护
     */
    @PreAuthorize(hasPermi = "system:title:edit")
    @Log(title = "CRM跑马灯维护", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmTitle crmTitle)
    {
        return toAjax(crmTitleService.updateCrmTitle(crmTitle));
    }

    /**
     * 删除CRM跑马灯维护
     */
	@PostMapping("remove")
    public AjaxResult remove(@RequestBody Map<String, Object> map)
    {
        Long id = Long.valueOf(map.get("id").toString());
        return toAjax(crmTitleService.deleteCrmTitleById(id));
    }

    /**
     * 跑马灯列表
     * */
    @PostMapping("/marquee")
    public TableDataInfo marquee(){
        startPage();
        List<CrmTitle> crmTitleList = crmTitleService.marquee();
        return getDataTable(crmTitleList);
    }

    /**
     * 添加跑马灯
     * */
    @PostMapping("/addMarquee")
    public AjaxResult addMarquee(HttpServletRequest request, @RequestBody Map<String, Object> map){
        String name = map.get("name").toString();
        int i;
        try {
            i = crmTitleService.addMarquee(request, name);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("添加成功");
        }
        else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 跑马灯显示
     * */
    @PostMapping("/show")
    public AjaxResult show(){
        String name = crmTitleService.show();
        return AjaxResult.success(name);
    }

    /**
     * 启用跑马灯
     * */
    @PostMapping("/enable")
    public AjaxResult enable(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        int i = crmTitleService.enable(id);
        if (i > 0){
            return AjaxResult.success("启用成功");
        }
        else {
            return AjaxResult.error("启用失败");
        }
    }
}
